﻿using ApplicationLibrary.SqlConnector;

namespace Plumber.Sales.ModuleData
{
    #region DRTemplatesByInvoice

    public class DRTemplatesByInvoice : DataReaderUpdater
    {
        private SqlABParameter p1, p2;
        private string code;
        TemplateType tempType;

        public DRTemplatesByInvoice(SqlABConnection sqlCN, bool updater)
            : base(sqlCN, updater)
        {
            myTable = PL_Templates.Name;
        }

        public bool Find(TemplateType temptype, string code)
        {
            tempType = temptype;
            this.code = string.Format("%{0}%", code);
            return base.Find();
        }

        protected override void CreateConnection()
        {
            myTable = PL_Templates.Name;

            p1 = new SqlABParameter("@p51", PL_Templates.Type);
            p2 = new SqlABParameter("@p52", PL_Templates.Code);

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_Templates.Name);
            qb.AddFrom(PL_Templates.Name);
            qb.AddCompare(PL_Templates.Type, p1);
            qb.AddCompare(PL_Templates.Combine, p2, "LIKE");

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            p1.Value = tempType;
            p2.Value = code;
        }
    }

    #endregion

    #region DRTemplates

    public class DRTemplates : DataReaderUpdater
    {
        private SqlABParameter p1, p2;
        private string code;
        TemplateType tempType;

        public DRTemplates(bool updater)
            : base(updater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_Templates.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p51", PL_Templates.Type);
            p2 = new SqlABParameter("@p52", PL_Templates.Code);

            qB.AddSelect();
            qB.AddFrom(PL_Templates.Name);
            qB.AddCompare(PL_Templates.Type, p1);
            qB.AddCompare(PL_Templates.Code, p2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(TemplateType temptype, string code)
        {
            this.tempType = temptype;
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = tempType;
            p2.Value = code;
        }
    }

    #endregion

    #region RRSentences

    public class RRSentences : DataReaderUpdater
    {
        public RRSentences(bool upater)
            : base(upater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_Sentences.Name;

            QueryBuilder qB = new QueryBuilder();

            qB.AddSelect();
            qB.AddFrom(PL_Sentences.Name);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region RRSentences

    public class DRMAXIDSentences : DataReaderUpdater
    {
        public DRMAXIDSentences()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_Sentences.Name;

            QueryBuilder qB = new QueryBuilder();

            qB.AddFunctionAs("MAX({0})", PL_Sentences.ID);
            qB.AddFrom(PL_Sentences.Name);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region DRSalesInvoices

    public class DRSalesInvoices : DataReaderUpdater
    {
        private SqlABParameter sqlP1,sqlP2;
        private string invoiceNo;
        private InvoiceType invType;

        public DRSalesInvoices(bool upater)
            : base(upater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_SalesInvoices.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", PL_SalesInvoices.InvoiceNo);
            sqlP2 = new SqlABParameter("@p52", PL_SalesInvoices.InvoiceType);

            qB.AddSelect();
            qB.AddFrom(PL_SalesInvoices.Name);
            qB.AddCompare(PL_SalesInvoices.InvoiceType, sqlP2);
            qB.AddCompare(PL_SalesInvoices.InvoiceNo, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(InvoiceType invType, string invoiceNo)
        {
            this.invoiceNo = invoiceNo;
            this.invType = invType;

            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = invoiceNo;
            sqlP2.Value = invType;
        }
    }

    #endregion

    #region DRQuotations

    public class DRQuotations : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string quotationNo;

        public DRQuotations(bool upater)
            : base(upater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_Quotations.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", PL_Quotations.QuotationNo);

            qB.AddSelect();
            qB.AddFrom(PL_Quotations.Name);
            qB.AddCompare(PL_Quotations.QuotationNo, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string quotationNo)
        {
            this.quotationNo = quotationNo;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = quotationNo;
        }
    }

    #endregion

    #region DRSalesInvoices

    public class DRCreditNoteByInvoices : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string invoiceRef;

        public DRCreditNoteByInvoices(bool upater)
            : base(upater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_SalesInvoices.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", PL_SalesInvoices.InvoiceRef);

            qB.AddSelect();
            qB.AddFrom(PL_SalesInvoices.Name);
            qB.AddCompare(PL_SalesInvoices.InvoiceRef, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string invoiceRef)
        {
            this.invoiceRef = invoiceRef;

            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = invoiceRef;
        }
    }

    #endregion
}